package com.westvalley.workflow.action.nc;

import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.action.BasisWe8Action;
import com.westvalley.common.action.result.ActionResult;
import com.westvalley.common.action.util.WorkFlowActionUtil;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.result.JsonResult;
import com.westvalley.service.api.nc.LoanService;
import com.westvalley.service.api.nc.bean.LoanDoc;
import com.westvalley.service.api.nc.bean.LoanDocBody;
import com.westvalley.service.api.nc.bean.LoanDocBodyItem;
import com.westvalley.service.api.nc.bean.LoanDocHead;

public class L001CreateAction extends BasisWe8Action{

	@Override
	protected ActionResult executeResult(RequestInfo requestInfo) {
		ActionResult result = ActionResult.me(requestInfo);
		JsonResult jsonResult = LoanService.me(initBean(requestInfo)).getJsonResult();
		if(!jsonResult.isFlag())
			throw new RuntimeException(jsonResult.getMsg());
		else{
			JSONObject resultJson = (JSONObject) jsonResult.getResult();
			Db.update("UPDATE "+mainTableName+" SET NCxgdh = ? WHERE requestId = ?", resultJson.getString("message"),requestInfo.getRequestid());
		}
		return result;
	}
	
	private LoanDoc initBean(RequestInfo requestInfo){
		LoanDoc bean = new LoanDoc();
		LoanDocHead head = new LoanDocHead();
		MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
		head.setLrr("oalrr01");																	//默认制单人
		head.setNcbillno(WorkFlowActionUtil.getFieldValue(mainTableInfo, "NCxgdh"));			//NC单号
		head.setUnitcode(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeunitcode"));	//公司编码
		head.setBilltypecode(WorkFlowActionUtil.getFieldValue(mainTableInfo, "billtype"));		//交易类型编码
		head.setDjrq(WorkFlowActionUtil.getFieldValue(mainTableInfo, "danjrq"));				//单据日期
		head.setZy(WorkFlowActionUtil.getFieldValue(mainTableInfo, "ywzy"));					//业务摘要
		head.setBzbm("CNY");																	//币种
		head.setYbje(WorkFlowActionUtil.getFieldValue(mainTableInfo, "yuanbje"));				//原币金额
		head.setBbhl("1.00");																	//汇率
		head.setZhrq(WorkFlowActionUtil.getFieldValue(mainTableInfo, "zuichkr"));				//最迟还款日期
		head.setFjzs(WorkFlowActionUtil.getFieldValue(mainTableInfo, "fujzs"));					//附件张数
		
		String loanCompany = WorkFlowActionUtil.getFieldValue(mainTableInfo, "loanCompany");	//借款公司编码
		Record comInfo = Db.findFirst("SELECT * FROM WV_NC_UNIT WHERE PK_CORP = ?", loanCompany);
		if(comInfo!=null) 
			head.setZfdwbm(StringKit.null2o(comInfo.get("UNITCODE")));		
		
		String unitbank = WorkFlowActionUtil.getFieldValue(mainTableInfo, "unitbank");			//银行帐号
		Record unitBankInfo = Db.findFirst("SELECT * FROM WV_NC_UNIT_BANKS WHERE ID = ?",unitbank);
		if(unitBankInfo!=null)
			head.setFkyhzh(StringKit.null2o(unitBankInfo.get("ACCOUNTCODE")));
		
		head.setJsfs(WorkFlowActionUtil.getFieldValue(mainTableInfo, "jiesfs1"));				//结算方式编码
		head.setDwbm(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financeunitcode"));		//借款人公司编码
		head.setJkrbm(WorkFlowActionUtil.getFieldValue(mainTableInfo, "financedeptcode"));		//借款人部门编码
		head.setJkbxr(WorkFlowActionUtil.getFieldValue(mainTableInfo, "workcode"));				//借款人编码
		
		String financebank = WorkFlowActionUtil.getFieldValue(mainTableInfo, "financebank");	//借款人银行帐号
		Record psnBanks = Db.findFirst("SELECT * FROM WV_NC_PSNDOC_BANK WHERE ID = ?",financebank);
		if(psnBanks!=null)
			head.setSkyhzh(StringKit.null2o(psnBanks.get("ACCOUNTCODE")));
		head.setOabillno(requestInfo.getRequestid());											//OA单据号
		bean.setHead(head);
		
		String incomeNum = WorkFlowActionUtil.getFieldValue(mainTableInfo, "incomeNum");		//收支项目编码
		//遍历借款明细
		DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(0);
		LoanDocBody body = new LoanDocBody();
		for(Row row : detailTable.getRow()){
			LoanDocBodyItem item = new LoanDocBodyItem();
			item.setSzxm(incomeNum);												//收支项目编码
			item.setAmount(WorkFlowActionUtil.getFieldValue(row, "baoxje"));		//借款金额
			item.setReason(WorkFlowActionUtil.getFieldValue(row, "baoxsy"));		//借支事由
			body.add(item);
		}
		bean.setBody(body);
		return bean;
	}
	
}
